package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserContract;
import com.lantosec.hmrp.modules.archives.mapper.UserContractMapper;
import com.lantosec.hmrp.modules.archives.service.IUserContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserContractVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 合同信息表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-09
 */
@Service
@Primary
@Log4j2
public class UserContractServiceImpl extends ServiceImpl<UserContractMapper, UserContract> implements IUserContractService {
    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Override
    public List<UserContractVO> getUserContract(String userId) {
        List<UserContractVO> userContractVOList = new ArrayList<>();
        //查询用户合同信息的最新一条数据
        List<UserContract> userContractList = this.list(new QueryWrapper<UserContract>().eq(UserContract.ColName.user_id.toString(), userId).orderByDesc(UserContract.ColName.create_time.toString()));
        userContractList.forEach(param->{
            UserContractVO userContractVO = getUserContract(param);
            userContractVOList.add(userContractVO);
        });
        return userContractVOList;
    }

    @Override
    public void saveUserContract(UserContractVO vo) {
        UserContract userContract = vo.convertToEntity();
        userContract.setUserContractId(UUIDUtils.get());
        //获取当前用户id和名称
        userContract.setCreateId(TokenUtil.getUserId());
        userContract.setCreateUser(TokenUtil.getName());
        userContract.setCreateTime(DateUtils.nowDate());
        userContract.setVerifyStatus(1);
        this.save(userContract);
    }

    @Override
    @Transactional
    public void updateUserContract(UserContractVO vo) {
        String userContractId = vo.getUserContractId();
        //新增一条数据
        //saveUserContract(vo);
        //修改这条数据的审核状态为2
        if (StringUtils.isNotEmpty(userContractId)) {
            UserContract userContract = vo.convertToEntity();
            userContract.setVerifyStatus(1);
            this.updateById(userContract);
        }else {
            saveUserContract(vo);
        }
    }

    @Override
    public List<UserContractVO> getUserContractVerify(String userId) {
        List<UserContractVO> userContractVOList = new ArrayList<>();
        //查询用户合同信息为未审核和审核中的
        List<UserContract> userContractList = this.list(new QueryWrapper<UserContract>().eq(UserContract.ColName.user_id.toString(), userId).in(UserContract.ColName.verify_status.toString(), 0, 2));
        userContractList.forEach(param -> {
            UserContractVO userContractVO = getUserContract(param);
            userContractVOList.add(userContractVO);
        });
        return userContractVOList;
    }

    @Override
    public void verifyUserContract(UserContractVO vo) {
        UserContract userContract = vo.convertToEntity();
        Integer verifyStatus = userContract.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userContract.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userContract.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userContract.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userContract.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userContract.setVerifyId(TokenUtil.getUserId());
        userContract.setVerifyUser(TokenUtil.getName());
        userContract.setVerifyTime(DateUtils.nowDate());
        this.updateById(userContract);
    }

    @Override
    public List<UserContractVO> getUserContractHistory(String userId) {
        List<UserContractVO> userContractVOList = new ArrayList<>();
        //查询用户基本信息为4历史记录的
        List<UserContract> userContractList = this.list(new QueryWrapper<UserContract>().eq(UserContract.ColName.user_id.toString(), userId).eq(UserContract.ColName.verify_status.toString(), 4)
                .orderByDesc(UserContract.ColName.create_time.toString()));
        userContractList.forEach(param -> {
            UserContractVO userContractVO = getUserContract(param);
            userContractVOList.add(userContractVO);
        });
        return userContractVOList;
    }

    /**
     * 组装用户合同信息
     *
     * @param userContract
     * @return
     */
    private UserContractVO getUserContract(UserContract userContract) {
        UserContractVO userContractVO = userContract.convertToVO();
        List<String> savePath = new ArrayList<>();
        try {
            //返回合同信息附件路径
            String[] attachments = userContract.getAttachment().split(",");
            for (int i = 0; i <= attachments.length; i++) {
                SysUploads sysUploads = iSysUploadsService.getById(attachments[i]);
                savePath.add(InitDataListener.uploadsUrl + sysUploads.getSavePath() + sysUploads.getFileName());
            }
            userContractVO.setSavePath(savePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userContractVO;
    }
}
